import Vue from 'vue'
import axios from 'axios'
import { Message } from 'element-ui'
axios.defaults.baseURL = 'http://127.0.0.1:8888/api/private/v1/'

const message = Message

axios.interceptors.request.use(config => {
    config.headers.Authorization = sessionStorage.getItem('token')
    return config
})

axios.interceptors.response.use(function (response) {
    // 在接收响应做些什么
    const config = response.config
    if ([200, 201].indexOf(response.data.meta.status) < 0) {
        if (config.rejectCall) config.rejectCall()
        message.error(response.data.meta.msg)
        return Promise.reject(response);
    }
    return response
}, function (error) {
    message.error(error)
    return Promise.reject(error);
});

Vue.prototype.$http = axios